<?php
namespace Admin\Controller;

class LicenseController extends AdminController
{
    protected function _initialize()
    {
		parent::_initialize ();
	}

	public function add()
	{
		if (IS_POST)
		{
			$strAddress_name = trim($_POST['address_name']);
			if (empty($strAddress_name))
			{
				$this->error('请填写上牌地地址');
			}
			$data = [
			'address_name' => $_POST['address_name'],
			'bank_id'      => $_POST['bank_id'],
			'group_id'     => $_POST['group_id'],
			'create_time'  => time(),
				];

			$result = $this->updateData($data, 'license');
			if($result)
			{
				$this->success('添加成功',U('License/index'));
			}else{
				return $this->error('添加失败');
			}
		}

		$_bankList   = $this->getList('*','bank','status = 0','id desc');
		$_departList = $this->getList('*','group','pid = 0','id desc');
		$this->assign('_bankList',$_bankList);
		$this->assign('_departList',$_departList);
		$this->display();
	}

	public function edit()
	{
		if (IS_POST)
		{
          	$data = [
			'address_name' => $_POST['address_name'],
			'bank_id'      => $_POST['bank_id'],
			'group_id'     => $_POST['group_id'],
			'status'       => $_POST['status'],
				];
	  		$result = M('license')->where('id='.I('post.id'))->save($data);
	  		if ($result)
	  		{
	  			$this->redirect('License/index');
	  		}
		} else {
			$info        = $this->getInfo('*','license','id='.I('get.id',0,'intval'));
			$_bankList   = $this->getList('*','bank','status = 0','id desc');
			$_departList = $this->getList('*','group','pid = 0','id desc');
			$this->assign('_bankList',$_bankList);
			$this->assign('_departList',$_departList);
			$this->assign('info',$info);
			$this->display('edit');
		}

	}

	public function index($p=1)
	{
		$p            = intval($p) > 0 ? $p : 1;
		$licenseModel = M('license l');
		$pagesize     = 15;//每页数量
		$offset       = $pagesize * ($p - 1);//计算记录偏移量

		$count        = $licenseModel->join('join app_bank as ab on ab.id = l.bank_id')->join('join app_group as ag on ag.id = l.group_id')->count();
		$list         = $licenseModel->field('l.*,ab.bank_name,ag.name')->join('join app_bank as ab on ab.id = l.bank_id')->join('join app_group as ag on ag.id = l.group_id')->order('l.id desc')->limit($offset . ',' . $pagesize)->select();
		$page         = new \Think\Page($count, $pagesize);
		$page         = $page->show();

		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->display();
	}

	public function detail()
	{
		$this->display();
	}

	public function add_info()
	{
		$this->display();
	}

	public function Rebate()
	{
		$this->display();
	}

	public function Complain_index()
	{
		$this->display();
	}

	public function Complain_deal()
	{
		$this->display();
	}

	public function Complain_del()
	{
		$this->display();
	}

}